package com.site.web.controller.war;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.site.war.service.ISiteWarningService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.site.common.annotation.Log;
import com.site.common.core.controller.BaseController;
import com.site.common.core.domain.AjaxResult;
import com.site.common.enums.BusinessType;
import com.site.war.domain.SiteTower;
import com.site.war.service.ISiteTowerService;
import com.site.common.utils.poi.ExcelUtil;
import com.site.common.core.page.TableDataInfo;

/**
 * 基础信息Controller
 *
 * @author site
 * @date 2023-10-11
 */
@RestController
@RequestMapping("/war/tower")
public class SiteTowerController extends BaseController
{
    @Autowired
    private ISiteTowerService siteTowerService;

    @Autowired
    private ISiteWarningService siteWarningService;

    /**
     * 查询基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('site:tower:list')")
    @GetMapping("/list")
    public TableDataInfo list(SiteTower siteTower)
    {
        startPage();
        List<SiteTower> list = siteTowerService.selectSiteTowerList(siteTower);
        return getDataTable(list);
    }

    /**
     * 导出基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('site:tower:export')")
    @Log(title = "基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SiteTower siteTower)
    {
        List<SiteTower> list = siteTowerService.selectSiteTowerList(siteTower);
        ExcelUtil<SiteTower> util = new ExcelUtil<SiteTower>(SiteTower.class);
        util.exportExcel(response, list, "基础信息数据");
    }

    /**
     * 获取基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('site:tower:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(siteTowerService.selectSiteTowerById(id));
    }

    /**
     * 新增基础信息
     */
    @PreAuthorize("@ss.hasPermi('site:tower:add')")
    @Log(title = "基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SiteTower siteTower)
    {
        return toAjax(siteTowerService.insertSiteTower(siteTower));
    }

    /**
     * 修改基础信息
     */
    @PreAuthorize("@ss.hasPermi('site:tower:edit')")
    @Log(title = "基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SiteTower siteTower)
    {
        return toAjax(siteTowerService.updateSiteTower(siteTower));
    }

    /**
     * 删除基础信息
     */
    @PreAuthorize("@ss.hasPermi('site:tower:remove')")
    @Log(title = "基础信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(siteTowerService.deleteSiteTowerByIds(ids));
    }

    /**
     * ZYJ
     * 根据工地id获取所有塔吊信息和预警信息
     * @param siteNumber
     * @return
     */
    @GetMapping("/gerTowerListBySiteNumber")
    public AjaxResult gerTowerListBySiteNumber(String siteNumber){
        //塔吊信息集合
        List<Map<String, Object>> towperList = siteTowerService.gerTowerListBySiteNumber(siteNumber);
        if (towperList!=null&&towperList.size()>0){
            for (Map<String, Object> map : towperList) {
                if (map!=null){
                    String towerNumber = (String) map.get("towerNumber");
                    List<Map<String, Object>> warningList = siteWarningService.getWarningByTowerNumber(towerNumber);
                    map.put("warningList",warningList);
                }
            }
        }
        return success(towperList);
    }
}
